import gradio as gr
import requests
import json

# 替换为你的 DeepSeek API 密钥
API_KEY = 'sk-xgxbqajfxbsfptqumdbsrtiakcisgochhzyltatkcqtarlvq'
API_URL = 'https://api.deepseek.com/v1/chat/completions'

def chat_with_deepseek(prompt):
    headers = {
        'Authorization': f'Bearer {API_KEY}',
        'Content-Type': 'application/json'
    }

    data = {
        "model": "deepseek-chat",  # 替换为你想使用的模型
        "messages": [
            {"role": "user", "content": prompt}
        ]
    }

    response = requests.post(API_URL, headers=headers, data=json.dumps(data))
    if response.status_code == 200:
        return response.json()['choices'][0]['message']['content']
    else:
        return f"Error: {response.status_code}, {response.text}"

# 创建 Gradio 界面
def gradio_chat(prompt):
    return chat_with_deepseek(prompt)

iface = gr.Interface(
    fn=gradio_chat,
    inputs="text",
    outputs="text",
    title="DeepSeek Chat",
    description="与 DeepSeek 进行对话"
)

iface.launch()